package com.stadsetjari_hi.Fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.stadsetjari_hi.R;
import com.stadsetjari_hi.transmissionclasses.Stadsetjari;

public class Transmit_Location_Fragment extends Fragment implements OnClickListener 
{
	private TextView lblStatus;
	private CheckBox rememberMe;
	private android.app.FragmentManager fm;
	private ImageView imgoff/*,imgon*/; 
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    		Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.v_transmit_location_frag, container, false);
		getActivity();
		imgoff = (ImageView)v.findViewById(R.id.offline);
//		imgon = (ImageView)v.findViewById(R.id.online);
		lblStatus = (TextView)v.findViewById(R.id.lblStatus);
		if (Stadsetjari.isTransmitting(getActivity())) lblStatus.setText("Transmission Online");
        Button btnTransmitData = (Button) v.findViewById(R.id.transmitLocationData);
        rememberMe = (CheckBox)v.findViewById(R.id.rememberlogin); 
        fm = getActivity().getFragmentManager();
        
        if (Stadsetjari.isTransmitting(getActivity())) 
        {	imgoff.setImageResource(R.drawable.online);
        	lblStatus.setText("Transmission Online");
        	btnTransmitData.setText("Stop Sending Location Data");
        }
        else
        {
        	imgoff.setImageResource(R.drawable.offline);
        	lblStatus.setText("Transmission Offline");
        }
        	
        if (Stadsetjari.isLoggedIn(getActivity()))rememberMe.setChecked(true);
        btnTransmitData.setOnClickListener(this);
        rememberMe.setOnClickListener(this);
        
        return v;
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId())
  		{
  			case R.id.rememberlogin:
  				if (getActivity().findViewById(R.id.Loggin_user_fragment)== null)
				{
  					if(!rememberMe.isChecked())
	  				{
		  				Stadsetjari.LogOutUser(getActivity());
	  				}
  					
					Loggin_Fragment logFragment = new Loggin_Fragment();
					logFragment.setArguments(getActivity().getIntent().getExtras());
					
					android.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
					fragmentTransaction.setCustomAnimations(R.animator.fade_in, R.animator.fade_out);
					fragmentTransaction.replace(R.id.myfragment, logFragment);
					fragmentTransaction.commit();
					
  					Stadsetjari.stopTransmittingLocationData(getActivity());
					lblStatus.setText("Transmission Offline");
	  				break;
				}
  			
  			case R.id.transmitLocationData:
  				 Button btnTransmitData = (Button) v.findViewById(R.id.transmitLocationData);
                
				if (!Stadsetjari.isTransmitting(getActivity())) 
				{

  					Stadsetjari.TransmitLocationData(getActivity());
					btnTransmitData.setText("Stop Sending Location Data");
					lblStatus.setText("Transmission Online");
					imgoff.setImageResource(R.drawable.online);
				}
				else 
				{
  					Stadsetjari.stopTransmittingLocationData(getActivity());
  					btnTransmitData.setText("Send Location Data");
					lblStatus.setText("Transmission Offline");
					imgoff.setImageResource(R.drawable.offline);
				}
			break;
  				
  		}
	}
}
